<template>
  <rx-layout>
    <div slot="left" width="220" showButton="true">
      <rx-fit>
        <rx-category-treeeditor
          cat-key="SQL_LOG"
          :edit="false"
          @handSelect="handSelect"
          :async="true"
        ></rx-category-treeeditor>
      </rx-fit>
    </div>
    <div slot="center" style>
      <rx-fit>
        <div slot="toolheader" foldheader="true" foldbtn="false" border="false">
          <div class="table-operator">
            <a-button type="primary" @click="clear">
                {{i18n('list.clearLog','清空日志')}}
            </a-button>
              <a-button type="primary" @click="delById(selectedRowKeys)">
                  {{$ti18('rxList.delBtn','删除')}}
              </a-button>
          </div>
          <span class="search-btn-box">
                <span class="search-btn" @click="searchshow"><i class="iconfont iconguolv"></i>
                    {{$ti18('rxList.leachBtn','过滤')}}
                </span>
           </span>
        </div>
          <div btnalign="right" slot="searchbar" v-if="fitSearch" @search="search" @cancel="cancel"
               @close="close">
              <a-form :layout="colLayout">
                  <a-row :gutter="24">
                      <a-col :span="colSpan">
                          <a-form-item :label="i18n('form.successOrNot','是否成功')">
                              <a-select :placeholder="i18n('form.successOrNot','是否成功','select')" v-model="queryParam.Q_IS_SUCCESS__S_LK" allowClear>
                                  <a-select-option value="YES">
                                      {{i18n('statusMap.success','成功')}}
                                  </a-select-option>
                                  <a-select-option value="NO">
                                      {{i18n('statusMap.fail','失败')}}
                                  </a-select-option>
                              </a-select>
                          </a-form-item>
                      </a-col>
                      <a-col :span="colSpan">
                          <a-form-item :label="i18n('form.executionTime','执行时间')">
                              <a-range-picker format="YYYY-MM-DD" @change="createTimeChange" />
                          </a-form-item>
                      </a-col>

                  </a-row>
              </a-form>
          </div>
        <rx-grid
                ref="table"
                :bordered="isBorder"
                :multiSelect="true"
                :allowRowSelect="true"
                :columns="columns"
                :defaultPageSize="10"
                url="/api-form/form/core/formSqlLog/query"
                :queryParam="queryParam"
                data-field="result.data"
                :pageSizeOptions="['10','30','40']"
                id-field="id"
                @selectChange="onSelectChange"
        >
          <span slot="createBy" slot-scope="{ text, record }">
            <rx-user-info :userId="text"></rx-user-info>
          </span>
          <span slot="isSuccess" slot-scope="{ text, record }">
            <a-tag :color="typeMap[text].color">
              <span>{{ typeMap[text].text }}</span>
            </a-tag>
          </span>
          <span slot="action" slot-scope="{ text, record }" class="actionIcons">
            <span @click.stop="logDetail(record)" :title="i18n('form.executionLog','执行日志')">
                {{i18n('form.executionLog','执行日志')}}
            </span>
          </span>
        </rx-grid>
      </rx-fit>
    </div>
  </rx-layout>
</template>

<script>
  import FormSqlLogApi from '@/api/form/core/formSqlLog'
  import FormSqlLogEdit from './FormSqlLogEdit'
  import FormSqlLogDetail from "./FormSqlLogDetail";
  import {BaseList,RxFit,RxGrid,Util,RxUserInfo } from "@lowcode/jpaas-common-lib";
  import RxCategoryTreeeditor from '@/components/redxun/rx-category-treeeditor'
  export default {
    name: 'FormSqlLogList',
    mixins: [BaseList],
    components: {
      RxCategoryTreeeditor,
      RxFit,
      RxGrid,
      RxUserInfo
    },
    data() {
      return {
        columns: [
          {
            title: this.$ti18('rxList.index','序号'),
            type: 'indexColumn',
            width: 30,
            dataIndex: 'serial',
            scopedSlots: {customRender: 'serial'}
          },
            {title: this.i18n('form.executor','执行人'), dataIndex: 'createBy', width: 100,scopedSlots: {customRender: 'createBy'}},
          {title: this.i18n('form.executionTime','执行时间'), dataIndex: 'createTime', width: 100},
          {title: this.i18n('form.executionLogResult','执行结果'), dataIndex: 'isSuccess', width: 50,scopedSlots: {customRender: 'isSuccess'}},
          {title: this.$ti18('rxList.action','操作'), width: 30, dataIndex: 'action', scopedSlots: {customRender: 'action'}}
        ],
        component:FormSqlLogEdit,
        comment:this.$ti18('form.sqlExecutionLog','SQL执行日志'),
        widthHeight:['800px','600px'],
        fitSearch:false,
        curTree: {},
        selectedRowKeys: [],
        selectedRows: [],
        typeMap: {
          'YES': { color: 'green', text: this.i18n('statusMap.success','成功') },
          'NO': { color: 'red', text: this.i18n('statusMap.fail','失败')  }
        },
        statisticConfig: {
            type:"static_form",
            table:"FORM_SQL_LOG",
            field:"TREE_ID_",
            whereConf:[]
        }
      }
    },
    methods:{
        i18n(name, text, key) {
            return this.$ti18(name, text, "formSqlLog", key);
        },
      //fsad
      delById(ids){
          if (ids.length==0){
              return;
          }
          FormSqlLogApi.del({ ids: ids.join(',') }).then(res=>{
              this.refreshGrid();
          });
      },
      onSelectChange(selectedRowKeys,selectedRows){
          this.selectedRows = selectedRows;
          this.selectedRowKeys = selectedRowKeys;
      },
      getListByParams(parameter){
        return FormSqlLogApi.query(parameter)
      },
      search(){
        this.$refs.table.loadData()
      },
      cancel(){
        this.resetSearch();
      },
      handSelect(curRow) {
        this.curTree = curRow
        var alias = curRow.alias
        this.loadData({ Q_TYPE__S_EQ: alias })
      },
      logDetail(record){
        Util.open({
          component: FormSqlLogDetail,
          curVm:this,
          widthHeight:['800px','600px'],
          title: this.i18n('form.executionLog','执行日志'),
          data:{
            pkId:record.pkId
          }
        },function () {})
      },
      clear(){
        if(!this.curTree.alias){
          this.$message.error(this.i18n('form.logClassification','日志分类！','select'));
          return;
        }
        FormSqlLogApi.delByType(this.curTree.alias).then(res=>{
          this.refreshGrid();
        });
      },
      createTimeChange(dates,dateStrings){
        this.queryParam['Q_CREATE_TIME__D_GE']=dateStrings[0];
        this.queryParam['Q_CREATE_TIME__D_LE']=dateStrings[1];
      },
      close(){
        //关闭按钮
        this.fitSearch = false ;
      },
    }
  }
</script>
<style scoped>

</style>

